package com.scedu.util;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/*
JDBC工具类
1.获取connentciong
2.释放资源
 */
public class JdbcUtil {
    /*
    获取connection
     */
    public  static Connection getConnetcion() throws Exception {
    /*
    不建议大家把配置信息写到代码中
     */
//        String url="jdbc:mysql://localhost:3306/springdata";
//        String user="root";
//        String password="root";
//        String driverClass="com.mysql.jdbc.Driver";
        InputStream inputStream= JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties=new Properties();
        properties.load(inputStream);
        String url=properties.getProperty("jdbc.url");
        String user=properties.getProperty("jdbc.user");
        String password=properties.getProperty("jdbc.password");
        String driverClass=properties.getProperty("jdbc.driverClass");
        Class.forName(driverClass);
        Connection connection= DriverManager.getConnection(url,user,password);
        return  connection;

    }
    public  static void release(ResultSet resultSet,
                                Statement statement,
                                Connection connection){
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
